package com.example.main;

import com.example.XmlUtil;
import com.example.test.Test01;
import com.example.test.Test02;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXValidator;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;
import org.springframework.web.util.HtmlUtils;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class Main01 {

    public static void main(String[] args) throws Exception{

        XMLErrorHandler xmlErrorHandler = new XMLErrorHandler();//xml默认的错误处理器

        SAXParserFactory factory = SAXParserFactory.newInstance();//获取基于SAX的解析器实例

        factory.setValidating(true);//解析器在解析xml内容

        factory.setNamespaceAware(true);//解析器对xml名称空间的支持

        SAXParser saxParser = factory.newSAXParser();//使用当前配置的工厂参数，创建一个新的SAXParser实例

        SAXReader xmlReader = new SAXReader();//创建一个读取文件


        saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2001/XMLSchema");
//        saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2001/XMLSchema-instance");
        saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource","file:"+"C:\\project\\code\\adev\\platform\\learnproject\\file-demo\\jackson-xml\\src\\main\\resources\\data\\combi-conversations_v1.2.xsd");

        SAXValidator validator = new SAXValidator(saxParser.getXMLReader());

        validator.setErrorHandler(xmlErrorHandler);//设置校验工具的错误处理器，当发生错误时，可以从处理对象中获取错误信息

        //开始校验xml
//        Document xmlDocument = xmlReader.read(new File("C:\\project\\code\\adev\\platform\\learnproject\\file-demo\\jackson-xml\\src\\main\\resources\\data\\test.xml"));
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        XmlUtil.xmlMapper.writeValue(bos, Test01.getConversations());
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        bos.writeTo(new FileOutputStream("C:\\project\\code\\adev\\platform\\learnproject\\file-demo\\jackson-xml\\src\\main\\resources\\data\\a.xml"));
        Document xmlDocument = xmlReader.read(bis);

        validator.validate(xmlDocument);
//        XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());

        /**
         * 判断校验xml文件是否存在错误信息
         */
        if (xmlErrorHandler.getErrors().hasContent()){
            Element errors = xmlErrorHandler.getErrors();//获取错误信息
            List<Node> list = errors.content();
//            writer.write(xmlErrorHandler.getErrors());

            int count = 0;//记录xsd校验xml出现的错误次数

            String resultError = "";//拼接校验的xml错误信息

            for (Node node : list) {
                String error = node.getText().split(":")[1];

                if (error.contains("元素") && error.contains("无效")){
                    count++;
                    resultError += error;
                }
            }

            System.out.println("共有"+count+"处错误");
            System.out.println(resultError);

        }

        ByteArrayOutputStream bos2=new ByteArrayOutputStream();
        XmlUtil.xmlMapper.writeValue(bos2, Test02.getConversations());
        ByteArrayInputStream bis2 = new ByteArrayInputStream(bos2.toByteArray());
        Document xmlDocument2 = xmlReader.read(bis2);
        validator.setErrorHandler(new XMLErrorHandler());
        validator.validate(xmlDocument2);
        XMLWriter writer2 = new XMLWriter(OutputFormat.createPrettyPrint());


        if (xmlErrorHandler.getErrors().hasContent()){
            Element errors = xmlErrorHandler.getErrors();//获取错误信息
            List<Node> list = errors.content();
            writer2.write(xmlErrorHandler.getErrors());

            int count = 0;//记录xsd校验xml出现的错误次数

            String resultError = "";//拼接校验的xml错误信息

            for (Node node : list) {
                String error = node.getText().split(":")[1];

                if (error.contains("元素") && error.contains("无效")){
                    count++;
                    resultError += error;
                }
            }

            System.out.println("共有"+count+"处错误");
            System.out.println(resultError);

        }
    }
}
